import api from '../../../api/index'
import uploadOss from '../../../utils/uploadFileOss/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    name: '',
    content: '',
    fileList: [],
    addimg:'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/community/music_add.png'
  },
  onLoad(options) {
    // const {file,title,logo} = options
    // console.log(file);
    // console.log(title);
    // if(logo) {
    //   this.setData({
    //     addimg: logo,
    //     file
    //   });
    // }
  },
  onShow() {
    console.log(wx.getStorageSync("logo"));
    let logo =  wx.getStorageSync("logo")
    console.log(logo);
    if(logo) {
      this.setData({
        addimg: logo,
      });
    }
  },
  onUnload: function() {
    var keys = ['logo', 'file'];

    keys.forEach(function(key) {
      wx.removeStorageSync(key);
    });
    console.log('删除成功');
  },
  goListClick() {
    wx.navigateTo({
      url: '/community/pages/misiclist/index'
    });
  },
  //文件选择
  afterRead(event) {
    console.log(event);
    const {file} = event.detail;
    console.log(file);
    var num = []
            num.push(file.url)
            console.log(num);
            console.log(num);
            this.uploadDIY(
                num,
                0,
                0,
                0,
                num.length,
                num
            ) 
},
/* 删除图片*/
deleteImg(e) {
    let fileList = this.data.fileList;
    fileList.splice(e.detail.index, 1);
    this.setData({fileList: fileList});
},
uploadDIY(filePaths, successUp, failUp, i, length, param) {
  // 正在上传
  // debugger
  param.doing = true // this.data.prove.doing = true;
  param.imagesUrl = []
  let that = this
  // wx.showLoading({
  //     title: `上传${i}/${length}张图片中`,
  //     duration: 1000
  // })

  uploadOss(filePaths[i])
      .then((res) => {
          successUp++
          i++
          param.imagesUrl.push(res.url)
          if (i === length) {
              // 全部上传成功
              console.log('图片上传成功后的url', param.imagesUrl, length)
              if (successUp === length) {
                  param.uploadSuccess = true
                  console.log('图片上传成功进入回调')
                  let img = {
                    url: param.imagesUrl,
                    name: '图片1',
                }
                let fileList = this.data.fileList;
                  fileList.push(img);
                  console.log(fileList);
                  let updatedData = fileList.map(item => {
                    const { url, name, ...rest } = item;
                    return { file: url[0], ...rest };
                  });
                  
                  updatedData = {images: updatedData}
                  this.setData({updatedData: updatedData});
                  console.log(updatedData);
                  this.setData({fileList: fileList});
                  // wx.hideLoading()
                  // that.savetmp(param.imagesUrl[0])
              } else {
                  // 图片上传部分失败
                  param.doing = false
                  wx.hideLoading()
                  wx.showToast({
                      title: '上传图片失败,请重新上传！',
                      icon: 'none',
                      duration: 2000
                  })
              }
          } else {
              // 递归调用uploadDIY函数
              that.uploadDIY(
                  filePaths,
                  successUp,
                  failUp,
                  i,
                  length,
                  param
              )
          }
      })
      .catch((e) => {
          console.log('---图片上传失败 catch--- e', e)
          param.doing = false
          failUp++
          wx.hideLoading()
          wx.showToast({
              title: '图片上传失败，请重新上传！',
              icon: 'none',
              duration: 2000
          })
      })
},
 
  formSubmit: function (e) {
    console.log(e);
    let _that = this;
    let name = e.detail.value.name;
    let content = e.detail.value.content;
    if (name == "") {
      wx.showModal({
        title: '提示',
        content: '标题不能为空!',
      })
      return false
    }
    else {
      this.setData({
        loading: true
      })
      var n = wx.getStorageSync("userinfo");
      let file = wx.getStorageSync("file");
      console.log(this.data.updatedData);
    let status = false;
    let updatedData = this.data.updatedData
    if(file) {
      updatedData.bgmusic = {file:file}
    }
      api.post('/api/hxhealth/forum/article/save', {
        data: {
          name,
          content:content?content:'',
          media: JSON.stringify(this.data.updatedData)
        }
    }).then(res => {
        wx.showToast({
            title: '提交成功',
        })
        wx.setStorageSync('logo', '');
        wx.setStorageSync('file', '');
        //提示框消失后返回上一级页面
        setTimeout(() => {
          wx.navigateBack({
            change: true,
          })
      }, 1200)
    }).catch(e => {
        console.log(e)
        wx.showToast({
          icon: 'error',
          duration: 1200,
            title: e.faultstring,
        })
    })
      return status;
    }
  },
})